import { updateUserInfo } from '@/store/dispatches.js';
import { mapState } from 'vuex';
import { STATIC_FILE_DOMAIN } from '@/constant/Config.js';
import moment from '@/utils/Moment.js'
import Service from '@/utils/Service.js';
import Tools from '@/utils/Tools.js';
const CommonMixin = {
	data() {
		return {
			errorImageSrc: '/static/images/common/default-image.png',
			userDefaultFace: '/static/images/user/default-face.png',
			staticFileDomain: STATIC_FILE_DOMAIN,
			// #ifndef APP-NVUE
			viewToken: this.$u.guid(),
			// #endif
			vipJoinPopup: false,
			disableToUserZone: false,
		}
	},
	computed: {
		...mapState({
			userInfo: state => state.user.info,
			userToken: state => state.user.token,
			permission: state => state.device.permission
		})
	},
	methods: {
		toPage(e) {
			console.log(e);
			const dataset = e.currentTarget.dataset
			if (!dataset.auth || (dataset.auth && this.userToken)) {
				this.navigateTo({
					url: dataset.url
				})
			} else {
				this.toLogin();
			}

		},
		startChat(to_id, type = 'single') {
			let url = ''
			to_id = String(to_id)
			if (this.userInfo.login_identity == 20 || (this.userInfo.login_identity == 10 && !!this.userInfo.is_vip)) {
				if (type == 'group' || to_id.indexOf('g_') >= 0) {
					url = '/pageChat/group/index?group_id=' + to_id
				} else {
					url = '/pageChat/single/index?user_id=' + to_id
				}
				this.navigateTo({ url })
			} else {
				this.vipJoinPopup = true;

			}

		},
		checkSwitchPage(url) {
			console.log(url)
			const checkUrl = String(url).split('?')[0];
			const app = getApp({
				allowDefault: true
			});
			return app.globalData.tabBarLinks.indexOf(checkUrl) >= 0;
		},
		navigateTo(opt) {
			if (opt.url) {
				if (opt.url.indexOf('https://') == 0 || opt.url.indexOf('http://') == 0) {
					uni.navigateTo({
						url: '/pageSupport/webview/index?url=' + encodeURIComponent(opt.url)
					})
				} else if (this.checkSwitchPage(opt.url)) {
					console.log(111)
					uni.switchTab(opt)
				} else {
					uni.navigateTo(opt)
				}
			}
		},
		redirectTo(opt) {
			if (this.checkSwitchPage(opt.url)) {
				uni.switchTab(opt)
			} else {
				uni.redirectTo(opt)
			}

		},
		reLaunch(opt = {}) {
			//TODO后面要根据当前角色确定重启路径
			if (!opt.url) {
				opt.url = '/pages/index/index'
			}
			uni.reLaunch(opt)
		},
		navigateBack(opt = {}) {
			if (getCurrentPages().length <= 1) {
				this.reLaunch();
			} else {
				uni.navigateBack(opt)
			}
		},
		toLogin() {
			Tools.toLoginPage();
		},
		isLogin() {
			return !!this.userToken;
		},
		visitUserZone(user_id, animationType = 'pop-in', assign = 0) {
			if (!this.disableToUserZone && user_id > 0) {
				this.disableToUserZone = true;
				if (assign > 0) {
					if (assign != 20) {
						this.navigateTo({ url: '/pageUser/zone/member?user_id=' + user_id, animationType })
					} else {
						this.navigateTo({ url: '/pageUser/zone/matchmaker?user_id=' + user_id, animationType })
					}
				} else {
					this.$u.api.getUserIdentity(user_id).then(res => {
						if (res.data.login_identity == 20) {
							this.navigateTo({ url: '/pageUser/zone/matchmaker?user_id=' + user_id, animationType })
						} else {
							this.navigateTo({ url: '/pageUser/zone/member?user_id=' + user_id, animationType })
						}
						setTimeout(() => {
							this.disableToUserZone = false;
						}, 100)
					}).catch(e => {
						this.disableToUserZone = false;
					})
				}

			}
		},
		// auto:自动化处理
		refreshUserInfo(param = {}) {
			param.auto = param.auto == undefined ? true : param.auto;
			const lnglatUpdate = (this.userInfo.longitude && this.userInfo.latitude) ? 0 : 1
			if (this.userToken) {
				this.$u.api.getUserBase(lnglatUpdate).then(res => {
					const userInfo = res.data.data;
					// #ifdef APP-PLUS
					const clientId = Service.getClientId();
					if (clientId != (userInfo.client_id || '')) {
						this.$u.api.submitClientId(clientId)
					}
					// #endif

					if (userInfo.is_vip == 1) {
						const vipEndTimeMoment = moment(userInfo.vip.end_time * 1000);
						const vipEndTimeDiff = vipEndTimeMoment.diff(moment(), 'days');
						this.$set(userInfo, 'vipEndTimeDiff', vipEndTimeDiff)
						if (vipEndTimeDiff < 7) {
							this.$set(userInfo, 'vipEndTimeToNow', vipEndTimeMoment.toNow(true) + '后')
						} else {
							this.$set(userInfo, 'vipEndTimeToNow', vipEndTimeMoment.format('YYYY-MM-DD'))
						}
					}
					updateUserInfo(userInfo);
					if (param.auto) {
						if (userInfo.province && userInfo.city && userInfo.district && (userInfo.login_identity == 10 || userInfo.login_identity == 20)) {
							setTimeout(() => {
								this.completeUserInfo(userInfo)
							}, 0)
						} else {
							this.navigateTo({
								url: '/pageUser/completion/index' + ((!userInfo.province || !userInfo.city || !userInfo.district) ? '' : '?step=2')
							})

						}

					}

				})
			}

		},
		completeUserInfo(data) {

		}

	}
};

export default CommonMixin;
